package com.gzk.hrm.consumer;

import com.gzk.hrm.to.EmailMessageTo;
import com.gzk.hrm.to.SmsMessageTo;
import com.gzk.hrm.to.SystemMessageTo;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

import static com.gzk.hrm.constants.RabbitMQConstants.*;

@Component
@Slf4j
public class MessageConsumer {


    /**
     * 监听短信
     * queues：从哪个队列消费消息
     * 指定消费者的容器工厂，使用json反序列化
     */
    @RabbitListener(queues = QUEUE_NAME_SMS,containerFactory = "rabbitListenerContainerFactory")
    public void handlerSmsMessage(@Payload SmsMessageTo smsMessageTo, Message message, Channel channel){
        List<String> phones = smsMessageTo.getPhones();
        String content = smsMessageTo.getContent();
        //@todo 发短信
        log.info("发送消息给{}，内容为{}",phones,content);
        System.out.println(content);
        log.info("短信发送记录保存到DB");

        //手动签收消息
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("消息签收失败！");
        }
    }

    /**
     * 监听邮件
     * @param emailMessageTo
     */
    @RabbitListener(queues = QUEUE_NAME_EMAIL,containerFactory = "rabbitListenerContainerFactory")
    public void handlerEmailMessage(@Payload EmailMessageTo emailMessageTo, Message message, Channel channel){
        List<String> emails = emailMessageTo.getEmails();
        String content = emailMessageTo.getContent();
        String title = emailMessageTo.getTitle();
        //@todo 发短信
        log.info("发送邮件给{}，内容为{}",emails,content);
        System.out.println(content);
        log.info("邮件发送记录保存到DB");

        //手动签收消息
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("消息签收失败！");
        }

    }

    /**
     * 监听站内信
     * @param systemMessageTo
     */
    @RabbitListener(queues = QUEUE_NAME_SYSTEM_MESSAGE,containerFactory = "rabbitListenerContainerFactory")
    public void handlerEmailMessage(@Payload SystemMessageTo systemMessageTo, Message message, Channel channel){
        List<Long> userIds = systemMessageTo.getUserIds();
        String content = systemMessageTo.getContent();
        String title = systemMessageTo.getTitle();
        //@todo 发短信
        log.info("发送站内信给{}，内容为{}",userIds,content);
        System.out.println(content);
        log.info("站内信发送记录保存到DB");

        //手动签收消息
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("消息签收失败！");
        }
    }


}
